//JavaScript Documentvar 
myApp =
{
		temp : null,
		currentPosition: null,
		cube:null,
		trackedObject:null,
		objInd:null,
		
		createContent : function()
		{

		},

		onArgonReady : function()
		{
			temp = 0;
			objInd =0;
			myApp.createContent();

			currentPosition = new Object();
			checkPoints.loadCheckPointsData();
			/*
			var floatCSS = document.getElementById("radarBar")

			radar.initCanvas();

			floatCSS.appendChild(radar.canvas);
			
			
			map.initialCanvas();
			var campusmapCSS = document.getElementById("map");
			campusmapCSS.appendChild(map.canvas);
			
			compass.initialCanvas();
			var compassDiv = document.getElementById("compass");
			compassDiv.appendChild(compass.canvas);
			*/
			//myApp.updateObject(objInd);	

			//ARGON.loadDataset("http://www.prism.gatech.edu/~xxie37/ar/Project.xml");
			//ARGON.onRender = myApp.onRender;
		},
		onDataSetLoaded : function(event)
		{
			var dataset, stonesTarget;
			var redCube, redMaterial, redGeometry;

			dataset      = event.dataset;
			stonesTarget = dataset.targets["history"];
			if (stonesTarget)
			{
				trackedObject        = new ARGON.TrackedObject();
				trackedObject.name   = "AttachedToStonesTarget"; 

				trackedObject.autoHideAfterFrames = 1;
				trackedObject.setTarget( stonesTarget );

				redGeometry = new THREE.CubeGeometry(100, 100, 20, 2, 2, 2);
				redMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000, shading: THREE.FlatShading, overdraw: true });
				redCube = new THREE.Mesh( redGeometry, redMaterial );
				redCube.position.z = 50.0;
				var divEl = document.createElement('div');
				divEl.id = "cssContent";
				divEl.style.width = "100px";
				divEl.style.height = "100px";
				divEl.style.backgroundColor = "red";
				divEl.style.position = 'absolute';
				divEl.style.fontSize = "16px";
				divEl.innerText = "Go to the Klaus building and find XXX";

				var cssObject;
				cssObject = new THREE.CSSObject(divEl); 
				cssObject.width = 100;
				cssObject.height = 100;
				cssObject.position.x = 0.0;
				cssObject.position.y = 0.0;
				cssObject.position.z = 20.0;
				cssObject.visible = false;

				//redCube.add(cssObject);
				trackedObject.add( cssObject );
			}
		},
		checkOrientation: function(){
			var deviceAt = ARGON.deviceAttitude;
			//console.log("start here");
			//console.log(deviceAt[0]);
			var z = new Array();
			z[0] = deviceAt[2];
			z[1] = deviceAt[6];
			z[2] = deviceAt[10];

			if(z[1]>0.9) { map.clearMap(); compass.clearcompass();}
			if(z[2]>0.9){ map.showMap(); compass.showcompass();}


			//if(z[1]>0.9) console.log("UP");
			//if(z[2]>0.9) console.log("LAY");

			/*	var str =   da[0] + " " + da[1] + " " + da[2] + " " + da[3] + "\n "
								   +da[4] + " " + da[5] + " " + da[6] + " " + da[7] + "\n "
								   +da[8] + " " + da[9] + " " + da[10] + " " + da[11] + "\n "
								   +da[12] + " " + da[13] + " " + da[14] + " " + da[15] + "\n ";*/
		},
		onRender: function(time) {
			var loc = navigator.geolocation.getCurrentPosition(myApp.showPosition);
			myApp.checkOrientation();
			compass.updatecompass();
			
			//var loc = myApp.getLocation();
			//console.log("test");
			//console.log("latitude"+loc.coords.latitude);
			//console.log("longitude"+loc.coords.longitude);
			myApp.checkObject();
		},
		checkObject: function(){
			if(trackedObject!= null){
				var curPos = trackedObject.threeObject3D.matrix.decompose()[0];
				if(!(curPos.x ==0 &&curPos.y ==0 && curPos.z==0) && objInd ==0){
					objInd = 1;
					myApp.updateObject(objInd);	
					console.log("Object Changed");			
				}
			}
		},

		showPosition: function(position)
		{

			//myApp.currentPosition.latitude =  position.coords.latitude;
			//myApp.currentPosition.longitude = position.coords.longitude;
			var dist = Geolocation.haversine(position.coords,checkPoints.checkPointsList[objInd]);
		//	radar.drawRadar(dist);
			setColor(dist);
			showInBigPic(dist);
			//console.log(temp);
			//var x=document.getElementById("demo");
			//x.innerHTML = temp + " dist "+ dist;
			//console.log(temp);
			//console.log("latitude"+position.coords.latitude);
			//console.log("longitude"+position.coords.longitude);
			//console.log("distance"+dist);
		},
		updateObject: function(ind){
		  	  var objectText = document.getElementById("objectText");
			  objectText.innerHTML = "Current Object:" + checkPoints.checkPointsList[ind].objectText;
			  objectText.addEventListener('touchstart', function(ev) {

                      if (ev.touches.item(0) == ev.targetTouches.item(0))
                      {
                          /**
                           * If the first touch on the surface is also targeting the
                           * "touchable" element, the code below should execute.
                           * Since targetTouches is a subset of touches which covers the
                           * entire surface, TouchEvent.touches >= TouchEvents.targetTouches
                           * is always true.
                           */
						    var touch = ev.targetTouches[0];
     						var x = touch.pageX;
     						var y = touch.pageY;
                           console.log('Hello Object Text');
						   console.log('x='+x+"  y=" + y );
                      }
			}, false);
		}        
};